# -*- coding: utf-8 -*-
from billing.models import Consumption ,Price ,RecordTime, DayRecord
from register.models import User
from django.db.models import Sum
from register.keystone_util import Keystone
import time
import datetime
import decimal
from decimal import Decimal
import logging

log=logging.getLogger('django')

def generate_all_user_consumption():
    
    print datetime.datetime.now()
    print "----月账单定时任务开始-----"
    
    keystoneclient = Keystone()
    v_client = keystoneclient.admin_login()
    
    recordTime = RecordTime.objects.get(id=1)
    if recordTime.modify_time is None:
        startTime = recordTime.record_time
    else:
        startTime = recordTime.modify_time
        
    endTime = datetime.datetime.now()
    print "时间范围为:"
    print startTime
    print endTime
    try:
        memoryPrice = Price.objects.filter(price_name='内存').filter(status=0)[0].unit_price
        diskPrice = Price.objects.filter(price_name='磁盘').filter(status=0)[0].unit_price
        cpuPrice = Price.objects.filter(price_name='vcpu').filter(status=0)[0].unit_price
    except:
        memoryPrice = Decimal(0)
        diskPrice = Decimal(0)
        cpuPrice = Decimal(0)
    
    monthRecordList = DayRecord.objects.filter(record_time__range = (startTime, endTime)).values("project_id").annotate(
            cpu_use = Sum("cpu_use"),disk_use=Sum("disk_use"),memory_use=Sum("memory_use"),cpu_consume=Sum("cpu_consume"),disk_consume=Sum("disk_consume"),memory_consume=Sum("memory_consume")
            ).values('project_id','cpu_consume','disk_consume','memory_consume','cpu_use','disk_use','memory_use')
    
    print monthRecordList
    
    for record in  monthRecordList:
        project_name = keystoneclient.get_tenant_by_projectId(v_client,record['project_id']).name
        try:
            last_remaining_money =   Consumption.objects.filter(project_id=record['project_id']).order_by('-record_time')[0].remaining_money
        except:
            last_remaining_money = Decimal(0)
        print last_remaining_money
        cpu_consume_money = record['cpu_consume']
        disk_consume_money = record['disk_consume']
        memory_consume_money = record['memory_consume']
        consume_money = Decimal(cpu_consume_money + disk_consume_money + memory_consume_money).quantize(Decimal('0.00'))
        remaining_money = Decimal(Decimal(last_remaining_money) - consume_money).quantize(Decimal('0.00'))
        memory_count = record['memory_use']
        cpu_count = record['cpu_use']
        disk_count = record['disk_use']
        try:
            con = Consumption(project_id=record['project_id'],project_name=project_name,cpu_price = cpuPrice,disk_price = diskPrice,memory_price = memoryPrice,
                                  consume_money = consume_money,cpu_use_count = cpu_count,memory_use_count=memory_count,disk_use_count=disk_count,
                                  remaining_money=remaining_money,cpu_consume_money=cpu_consume_money,disk_consume_money=disk_consume_money,memory_consume_money=memory_consume_money,lastrecord_time = startTime) 
            con.save()
            print "保存数据正常"
        except:
            print "存储数据异常"
    else:
        try:
            recordTime.modify_time=datetime.datetime.now()
            recordTime.save()
            print "执行数据修改"
        except:
            print "存储时间异常"
            
    print datetime.datetime.now()
    print "定时任务结束-----"
    return True